package main;

import java.util.ArrayList;
import java.util.List;


import org.testng.ITestContext;
import org.testng.ITestResult;
import org.testng.TestListenerAdapter;

public class TestListener extends TestListenerAdapter {

	private List<ITestResult> listePass;
	private List<ITestResult> listeFail;
	private List<ITestResult> listeSkip;

	/* (non-Javadoc)
	 * @see org.testng.TestListenerAdapter#onStart(org.testng.ITestContext)
	 */
	@Override
	public void onStart(ITestContext testContext) {
		// TODO Auto-generated method stub
		super.onStart(testContext);
	}

	/* (non-Javadoc)
	 * @see org.testng.TestListenerAdapter#onFinish(org.testng.ITestContext)
	 */
	@Override
	public void onFinish(ITestContext testContext) {
		System.out.println("All tests are finished");
		
		
		System.out.println("\nPASSED TESTS: ");
		listePass =this.getPassedTests();
		if(listePass.size()==0) System.out.println("	EMPTY SET");
		else
		{
		for(ITestResult t : listePass)
		{
			System.out.print("	"+t.getName());
			this.printParams(t);
		}
		}
		
		System.out.println("\nFAILED TESTS: ");
		listeFail =this.getFailedTests();
		if(listeFail.size()==0) System.out.println("	EMPTY SET");
		else
		{
		for(ITestResult t : listeFail)
		{
			System.out.print("	"+t.getName());
			this.printParams(t);
		}
		}
		
		System.out.println("\nSKIPPED TESTS: ");
		listeSkip =this.getSkippedTests();
		if(listeSkip.size()==0) System.out.println("	EMPTY SET");
		else
		{
		for(ITestResult t : listeSkip)
		{
			System.out.print("	"+t.getName());
			this.printParams(t);
		}
		}
		super.onFinish(testContext);
	}

	/* (non-Javadoc)
	 * @see org.testng.TestListenerAdapter#getFailedTests()
	 */
	@Override
	public List<ITestResult> getFailedTests() {
		// TODO Auto-generated method stub
		return super.getFailedTests();
	}

	/* (non-Javadoc)
	 * @see org.testng.TestListenerAdapter#getPassedTests()
	 */
	@Override
	public List<ITestResult> getPassedTests() {
		// TODO Auto-generated method stub
		return super.getPassedTests();
	}

	/* (non-Javadoc)
	 * @see org.testng.TestListenerAdapter#getSkippedTests()
	 */
	@Override
	public List<ITestResult> getSkippedTests() {
		// TODO Auto-generated method stub
		return super.getSkippedTests();
	}
	
	
	public ArrayList<String> getParam(ITestResult t)
	{
		Object[] set=t.getParameters();
		ArrayList<String> list=new ArrayList<String>();
		
		for(int i=0;i<set.length;i++)
		{
			list.add(set[i].toString());
		}
		
		
		return list;
	}
	
	public void printParams(ITestResult t)
	{
		ArrayList<String>  liste=getParam(t);
		System.out.print("(");
		for(String s : liste)
		{
			System.out.print(s+",");
		}
		System.out.print(")\n");
	}

}
